//
//  PersonViewController.m
//  Gabber
//
//  Created by 陆惠国 on 11-11-3.
//  Copyright (c) 2011年 恒生聚源. All rights reserved.
//

#import "CardViewController.h"
#import "XMPPClient.h"

@implementation CardViewController

@synthesize jid = _jid;
@synthesize vcard = _vcard;

- (id)initWithJidStr:(NSString*)jidStr
{
    
    if ((self = [super init])) {
        
        self.jid = [XMPPJID jidWithString:jidStr];

        
        self.vcard = [[[XMPPClient sharedInstance] xmppvCardTempModule] fetchvCardTempForJID: self.jid];
        
        if(self.vcard != nil){
            ABRecordRef person = [self.vcard createPerson];
            //self.unknownPersonViewDelegate = self;
            self.personViewDelegate = self;
            self.displayedPerson = person;
            //self.allowsAddingToAddressBook = YES;
            self.allowsActions = YES;
            
            BOOL isMe = [self.jid isEqualToJID:[[[XMPPClient sharedInstance] xmppStream] myJID] options:XMPPJIDCompareBare];
            
            self.allowsEditing = isMe;
            self.title = jidStr;

            CFRelease(person);
            
            
        }
        
        [[[XMPPClient sharedInstance] xmppvCardTempModule] fetchvCardTempForJID: self.jid useCache:NO];

        

    }
    return self;   
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

- (void)dealloc {
    
    TT_RELEASE_SAFELY(_jid);
    TT_RELEASE_SAFELY(_vcard);
    [super dealloc];
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)viewDidUnload
{
    DDLogVerbose(@"%@: %@", THIS_FILE, THIS_METHOD);

    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}
- (void)viewWillDisappear:(BOOL)animated;
{
    
    DDLogVerbose(@"%@: %@", THIS_FILE, THIS_METHOD);
    
    if (self.allowsEditing){
        [self.vcard updateFromPerson:self.displayedPerson];
        [[[XMPPClient sharedInstance] xmppvCardTempModule] updateMyvCardTemp:self.vcard];
    }
    [super viewWillDisappear:animated];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    //return (interfaceOrientation == UIInterfaceOrientationPortrait);
    if (TTIsPad()) {
        return YES;
        
    } else {
        return interfaceOrientation == UIInterfaceOrientationPortrait;
    }
}

- (void)unknownPersonViewController:(ABUnknownPersonViewController *)unknownCardViewController didResolveToPerson:(ABRecordRef)person
{
    
}

- (BOOL)personViewController:(ABPersonViewController *)personViewController shouldPerformDefaultActionForPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier
{
    DDLogVerbose(@"%@: %@", THIS_FILE, THIS_METHOD);
    
    return YES;
}

@end
